const wxHttpHandler = require('../../utils/wxHttpHandler.js');
const api = require('../../APIConfig/apiConfig.js');

Page({
  data: {
    lineDatas: [],
    lineName: '',
    indicatorIndex: 1, //1 线路查询   2 站点查询
  },

  onLoad: function (options) {
    this.setData({
      lineName: options.keyWords,
      indicatorIndex: options.indicatorIndex
    })
    if (this.data.indicatorIndex == 1) {
      this.queryRoute()
      wx.setNavigationBarTitle({
        title: '线路查询',
      })
    } else {
      if (options.map) {
        wx.setNavigationBarTitle({
          title: options.keyWords,
        })
      } else
        wx.setNavigationBarTitle({
          title: '站点查询',
        })
      this.queryStation()
    }
  },
  onShow: function () {},

  toLineDetails: function (e) {
    console.log(e.currentTarget.dataset.a)
    if (e.currentTarget.dataset.b == null || e.currentTarget.dataset.a == null) {
      wx.showToast({
        title: '参数异常',
      })
      return
    }
    wx.navigateTo({
      url: '../lineDetails/lineDetails?routeId=' + e.currentTarget.dataset.a + '&direction=' + e.currentTarget.dataset.b + '&lineName=' + e.currentTarget.dataset.c,
    })
  },

  //获取推荐线路
  queryRoute: function (e) {
    let data = {
      routeName: this.data.lineName
    }
    const that = this
    wx.showLoading()
    wxHttpHandler({
      type: 'get',
      path: api.queryRoute,
      data: data,
      fn: function (res) {
        if (res.code == 0) {
          that.setData({
            lineDatas: res.data
          })
          wx.hideLoading()
        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },


  //获取推荐站点
  queryStation: function () {
    let data = {}
    const that = this
    wx.showLoading()
    wxHttpHandler({
      type: 'get',
      path: api.queryStation,
      data: data,
      fn: function (res) {
        if (res.code == 0) {
          that.setData({
            lineDatas: res.data
          })
          wx.hideLoading()
        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },


})